#include "vk_texture_sampler.h"


namespace vkrender
{
	VulkanTextureSampler::VulkanTextureSampler(const std::shared_ptr<VulkanDeviceContext>& context)
	{
		this->context = context;
		sampler = VK_NULL_HANDLE;

		createSampler();
	}

	VulkanTextureSampler::~VulkanTextureSampler()
	{
		vkDestroySampler(context->device, sampler, nullptr);
	}

	void VulkanTextureSampler::createSampler()
	{
		VkSamplerCreateInfo samplerInfo = {};
		samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
		samplerInfo.magFilter = VK_FILTER_LINEAR;
		samplerInfo.minFilter = VK_FILTER_LINEAR;
		samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
		samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
		samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
		samplerInfo.anisotropyEnable = VK_TRUE;
		
		VkPhysicalDeviceProperties properties = {};
		vkGetPhysicalDeviceProperties(context->physicalDevice, &properties);
		samplerInfo.maxAnisotropy = properties.limits.maxSamplerAnisotropy;

		samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
		samplerInfo.unnormalizedCoordinates = VK_FALSE;
		samplerInfo.compareEnable = VK_FALSE;
		samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
		samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
		samplerInfo.mipLodBias = 0.0f;
		samplerInfo.minLod = 0.0f;
		samplerInfo.maxLod = 0.0f;

		VkResult res = vkCreateSampler(context->device, &samplerInfo, nullptr, &sampler);
		if (res != VK_SUCCESS)
			std::cout << "failed to create sampler\n";
	}
}
